<?php
class MaozhuaController extends Zend_Controller_Action
{
    function preDispatch()
    {
		if(!$_SESSION['identity'])
        {
            $this->_redirect("/login");
        }
		
         //Make menu
         $mod_menu = new Db_Tables2_BDictMenu();
         $this->view->main_menu = $mod_menu->MenuDecolation();
        
        $mod_params = new Db_Tables2_BDictParams();
        $this->view->sys_name = $mod_params->GetSysName();
    }
	
    function indexAction()
	{
		$this->_redirect("/maozhua/users");
	}
	
    function usersAction()
    {
		$params = $this->_request->getParams();

		$this->view->title = "用户列表";
		
		$mod_t_area = new Db_Tables2_TArea();
		$this->view->provinces = $mod_t_area->GetProvinces();

		$params_pool = array();
		$mod_tables = new Db_Joins_TablesJoins();
    	
    	if($params['provinceId'])
    	{
    		$mod_tables->provinceId = $params['provinceId'];
    		$this->view->provinceId = $params['provinceId'];
    		$params_pool[] = "provinceId=".$params['provinceId'];
			
			$this->view->init_cities = $mod_t_area->GetCities($params['provinceId']);
    	}
    	
    	if($params['cityId'])
    	{
    		$mod_tables->cityId = $params['cityId'];
    		$this->view->cityId = $params['cityId'];
    		$params_pool[] = "cityId=".$params['cityId'];
    	}
		
		if($params['status'] && !empty($params['status']))
    	{
			$status_str = implode("_", $params['status']);
    		$mod_tables->status_str = $status_str;
    		$this->view->status_str = $status_str;
    		$params_pool[] = "status_str=".$status_str;
			$this->view->status_str = $params['status'];
    	}elseif($params['status_str'])
		{
			$status = explode("_", $params['status_str']);
    		$mod_tables->status_str = $params['status_str'];
    		$this->view->status_str = $params['status_str'];
    		$params_pool[] = "status_str=".$params['status_str'];
			$this->view->status_arr = $status;
		}
		
		if($params['keyword'])
    	{
    		$mod_tables->keyword = $params['keyword'];
    		$this->view->keyword = $params['keyword'];
    		$params_pool[] = "keyword=".$params['keyword'];
    	}
		
		if(!$params['page_id'])
    	{
    		$page_id = 1;
    	}else{
    		$page_id = $params['page_id'];
    	}

    	if($page_id)
    	{
    		$mod_tables->page_id = $page_id;
    		$this->view->page_id = $page_id;
    	}

		$this->view->data = $result = $mod_tables->GetMaozhuaUsers();

		//make pagination
    	if(!empty($params_pool))
    	{
    		$params_string = implode("&", $params_pool);
    	}else{
    		$params_string = "";
    	}

    	if(1 == $page_id)
    	{
    		$first_page_string = "";
    		$previous_page_string = "";
    	}else{
    		$first_page_string = "<a href='?page_id=1&".$params_string."'>最前页</a>&nbsp;&nbsp;&nbsp;";
    		$previous_page_string = "<a href='?page_id=".($page_id-1)."&".$params_string."'>上一页</a>&nbsp;&nbsp;&nbsp;";
    	}

    	if($page_id < $result['total_pages'] )
    	{
    		$next_page_string = "<a href='?page_id=".($page_id+1)."&".$params_string."'>下一页</a>&nbsp;&nbsp;&nbsp;";
    		$last_page_string = "<a href='?page_id=".$result['total_pages']."&".$params_string."'>最后页</a>&nbsp;&nbsp;&nbsp;";
    	}else{
    		$next_page_string = "";
    		$last_page_string = "";
    	}

		$this->view->params_string = $params_string;
    	$this->view->pagination = $first_page_string.$previous_page_string.$next_page_string.$last_page_string;

		if($params['msg'])
		{
				switch ($params['msg'])
				{
					case 1:
						$this->view->msg = "更新成功";
						break;
					case 2:
						$this->view->msg = "更新失败";
						break;
				}
		}
	}
	
	function editUserAction()
	{
		$params = $this->_request->getParams();
		
		if($params['user_id'])
		{
                        
			$this->view->user_id = $params['user_id'];
			
			$maozhua_wb = Zend_Registry::get('maozhua_wb');
			$select = $maozhua_wb->select();
			$select->from("users", "*");
			$select->where("user_id = ?", $params['user_id']);
			
			$row = $maozhua_wb->fetchRow($select);
			
			if($row)
			{
				$this->view->title = "编辑".$row['user_name'];
				$this->view->data = $row;
				$mod_t_area = new Db_Tables2_TArea();
				$this->view->init_cities = $mod_t_area->GetCities($row['province_id']);
				
				$mod_params = new Db_Tables2_BDictParams();
				$sales_role_id = array();
				$sales_role_id[] = $mod_params->GetVal("LEVEL-1");
				$sales_role_id[] = $mod_params->GetVal("LEVEL-2");

				$mod_user = new Db_Tables2_User();
				$this->view->sales = $mod_user->GetUsersByRole($sales_role_id);
				
				$mod_accounts = new Db_Tables2_Accounts();
				$this->view->accounts = $mod_accounts->GetAccounts($params['user_id']);
			}else{
				$this->_redirect("/maozhua/users/msg/2");
			}
		}else{
			$this->_redirect("/maozhua/users/msg/2");
		}
		
		$mod_t_area = new Db_Tables2_TArea();
		$this->view->provinces = $mod_t_area->GetProvinces();
		
		if($params['msg'])
		{
			switch ($params['msg'])
			{
				case 1:
					$this->view->msg = "用户信息修改成功。<a href='/maozhua/users'>【返回用户列表】</a>";
					break;
			}
		}
	}
	
	function editUserSubmitAction()
	{
		$params = $this->_request->getParams();
		
//		$mod_ext = new Sys_Extensions();
//		$mod_ext->FormatArray($params);
//		die;
		
		if($params['user_id'])
		{
                    $maozhua_wb = Zend_Registry::get('maozhua_wb');
              
			$data = array(
				"user_name" => trim($params['user_name']),
				"cellphone" => trim($params['cellphone']),
				"expired_date" => trim($params['expired_date']),
				"start_date" => trim($params['start_date']),
				"emp_id" => trim($params['emp_id']),
				"status" => trim($params['status']),
				"province_id" => trim($params['provinceId']),
				"city_id" => trim($params['cityId']),
                                "czht_dealer_id" =>trim($params['czht_dealer_id']),
                                "contract_sid"=>trim($params['contract_sid'])
			);
			
			if(trim($params['pwd']))
			{
				$data['pwd_sha1'] = sha1(trim($params['pwd']));
			}
			
		
			$where = "user_id = ".$params['user_id'];
			$maozhua_wb->update("users", $data, $where);
			
			//初始化汽车之家
			$select = $maozhua_wb->select();
			$select->from("accounts", "*");
			$select->where("user_id = ?", $params['user_id']);
			$select->where("account_type = ?", 1);
			
			$row = $maozhua_wb->fetchRow($select);
			
			if($row)
			{
				$data = array(
					"user_name" => trim($params['type1_user_name']),
					"dealer_id" => trim($params['type1_dealer_id']),
					"cookies" => NULL,
					"err_login_times" => 0
				);
				
				if(trim($params['type1_pwd']))
				{
					$data['pwd'] = trim($params['type1_pwd']);
				}
				
				if(trim($params['type1_dealer_id']) && trim($params['type1_user_name']) && (trim($params['type1_pwd']) || $row['pwd']) && $params['type1_status'] && $params['status'])
				{
					$data['status'] = 1;
					
					if(!$row['server_id'])
					{
						$mod_accounts = new Db_Tables2_Accounts();
						$data['server_id'] = $mod_accounts->AvailableServerId(1);
					}
				}else{
					$data['status'] = 0;
					$data['server_id'] = NULL;
				}
				
				$where = "account_id = ".$row['account_id'];
				$maozhua_wb->update("accounts", $data, $where);
				
				$mod_log1 = new Db_Tables2_Log1();
				$mod_log1->AddLog("商务系统 修改账号信息：account_id=".$row['account_id']." user_name=".trim($params['type1_user_name'])." pwd=".trim($params['type1_pwd']));
			}else{
				$data = array(
					"user_id" => $params['user_id'],
					"account_type" => 1,
					"user_name" => trim($params['type1_user_name']),
					"ctime" => date("Y-m-d H:i:s"),
					"dealer_id" => trim($params['type1_dealer_id']),
					"province_id" => trim($params['provinceId']),
					"city_id" => trim($params['cityId']),
				);
				
				if(trim($params['type1_pwd']))
				{
					$data['pwd'] = trim($params['type1_pwd']);
				}
				
				if(trim($params['type1_dealer_id']) && trim($params['type1_user_name']) && trim($params['type1_pwd']) && $params['type1_status'] && $params['status'])
				{
					$data['status'] = 1;
					
					$mod_accounts = new Db_Tables2_Accounts();
					$data['server_id'] = $mod_accounts->AvailableServerId(1);
				}else{
					$data['status'] = 0;
				}

				$maozhua_wb->insert("accounts", $data);
			}

			//初始化汽车之家APP
			$select = $maozhua_wb->select();
			$select->from("accounts", "*");
			$select->where("user_id = ?", $params['user_id']);
			$select->where("account_type = ?", 4);
			
			$row = $maozhua_wb->fetchRow($select);
			
			if($row)
			{
				$data = array(
					"user_name" => trim($params['type4_user_name']),
					"dealer_id" => trim($params['type4_dealer_id'])
				);
				
				if(trim($params['type4_pwd']))
				{
					$data['pwd'] = trim($params['type4_pwd']);
				}
				
				if(trim($params['type4_user_name']) && (trim($params['type4_pwd']) || $row['pwd']) && $params['type4_status'] && $params['status'])
				{
					$data['status'] = 1;
				}else{
					$data['status'] = 0;
				}
				
				$where = "account_id = ".$row['account_id'];
				$maozhua_wb->update("accounts", $data, $where);
				
				$mod_log1 = new Db_Tables2_Log1();
				$mod_log1->AddLog("商务系统 修改账号信息：account_id=".$row['account_id']." user_name=".trim($params['type4_user_name'])." pwd=".trim($params['type4_pwd']));
			}else{
				$data = array(
					"user_id" => $params['user_id'],
					"account_type" => 4,
					"user_name" => trim($params['type4_user_name']),
					"ctime" => date("Y-m-d H:i:s"),
					"dealer_id" => trim($params['type4_dealer_id']),
					"province_id" => trim($params['provinceId']),
					"city_id" => trim($params['cityId'])
				);
				
				if(trim($params['type4_pwd']))
				{
					$data['pwd'] = trim($params['type4_pwd']);
				}
				
				if(trim($params['type4_user_name']) && trim($params['type4_pwd']) && $params['type4_status'] && $params['status'])
				{
					$data['status'] = 1;
				}else{
					$data['status'] = 0;
				}
				$maozhua_wb->insert("accounts", $data);
			}

			//初始化易车
			$select = $maozhua_wb->select();
			$select->from("accounts", "*");
			$select->where("user_id = ?", $params['user_id']);
			$select->where("account_type = ?", 2);
			
			$row = $maozhua_wb->fetchRow($select);

			if($row)
			{
				$data = array(
					"user_name" => trim($params['type2_user_name']),
					"dealer_id" => trim($params['type2_dealer_id']),
					"cookies" => NULL,
					"err_login_times" => 0
				);

				if(trim($params['type2_pwd']))
				{
					$data['pwd'] = trim($params['type2_pwd']);
				}
				
				if(trim($params['type2_dealer_id']) && trim($params['type2_user_name']) && (trim($params['type2_pwd']) || $row['pwd']) && $params['type2_status'] && $params['status'])
				{
					$data['status'] = 1;
					
					if(!$row['server_id'])
					{
						$mod_accounts = new Db_Tables2_Accounts();
						$data['server_id'] = $mod_accounts->AvailableServerId(2);
					}
				}else{
					$data['status'] = 0;
					$data['server_id']=NULL;
                    $data['cookies']=NULL;
				}
				$where = "account_id = ".$row['account_id'];
				$maozhua_wb->update("accounts", $data, $where);
				
				$mod_log1 = new Db_Tables2_Log1();
				$mod_log1->AddLog("商务系统 修改账号信息：account_id=".$row['account_id']." user_name=".trim($params['type2_user_name'])." pwd=".trim($params['type2_pwd']));
			}else{
				if($params['cityId'])
				{
					$dealers_db = Zend_Registry::get("dealers_db");
					$dselect = $dealers_db->select();
					$dselect->from("t_area", array("yicheProvinceId", "yicheCityId"));
					$dselect->where("cityId = ?", $params['cityId']);
					$drow = $dealers_db->fetchRow($dselect);
				}else{
					$drow = NULL;
				}
				
				$data = array(
					"user_id" => $params['user_id'],
					"account_type" => 2,
					"user_name" => trim($params['type2_user_name']),
					"ctime" => date("Y-m-d H:i:s"),
					"dealer_id" => trim($params['type2_dealer_id'])
				);
				
				if($drow)
				{
					$data['province_id'] = $drow['yicheProvinceId'];
					$data['city_id'] = $drow['yicheCityId'];
				}
				
				if(trim($params['type2_pwd']))
				{
					$data['pwd'] = trim($params['type2_pwd']);
				}
				
				if(trim($params['type2_dealer_id']) && trim($params['type2_user_name']) && trim($params['type2_pwd']) && $params['type2_status'] && $params['status'])
				{
					$data['status'] = 1;
					
					$mod_accounts = new Db_Tables2_Accounts();
					$data['server_id'] = $mod_accounts->AvailableServerId(2);
				}else{
					$data['status'] = 0;
				}

				$maozhua_wb->insert("accounts", $data);
			}


            //初始化易车APP
            $select = $maozhua_wb->select();
            $select->from("accounts", "*");
            $select->where("user_id = ?", $params['user_id']);
            $select->where("account_type = ?", 5);

            $row = $maozhua_wb->fetchRow($select);

            if($row)
            {
                $data = array(
                    "user_name" => trim($params['type5_user_name']),
                    "dealer_id" => trim($params['type5_dealer_id'])
                );

                if(trim($params['type5_pwd']))
                {
                    $data['pwd'] = trim($params['type5_pwd']);
                }

                if(trim($params['type5_user_name']) && (trim($params['type5_pwd']) || $row['pwd']) && $params['type5_status'] && $params['status'])
                {
                    $data['status'] = 1;
                }else{
                    $data['status'] = 0;
                }

                $where = "account_id = ".$row['account_id'];
                $maozhua_wb->update("accounts", $data, $where);

                $mod_log1 = new Db_Tables2_Log1();
                $mod_log1->AddLog("商务系统 修改账号信息：account_id=".$row['account_id']." user_name=".trim($params['type5_user_name'])." pwd=".trim($params['type5_pwd']));
            }else{
                $data = array(
                    "user_id" => $params['user_id'],
                    "account_type" => 5,
                    "user_name" => trim($params['type5_user_name']),
                    "ctime" => date("Y-m-d H:i:s"),
                    "dealer_id" => trim($params['type5_dealer_id']),
                    "province_id" => trim($params['provinceId']),
                    "city_id" => trim($params['cityId'])
                );

                if(trim($params['type5_pwd']))
                {
                    $data['pwd'] = trim($params['type5_pwd']);
                }

                if(trim($params['type5_user_name']) && trim($params['type5_pwd']) && $params['type5_status'] && $params['status'])
                {
                    $data['status'] = 1;
                }else{
                    $data['status'] = 0;
                }
                $maozhua_wb->insert("accounts", $data);
            }


			
			$this->_redirect("/maozhua/users/msg/1");
		}else{
			$this->_redirect("/maozhua/users/msg/2");
		}
	}
	
	function appIcsAction()
	{
            $params = $this->_request->getParams();
            $this->view->title = "汽车之家APP状态";

            $maozhua_wb = Zend_Registry::get('maozhua_wb');
            $params_pool = array();
            $mod_tables = new Db_Joins_TablesJoins();

            if(!$params['page_id'])
            {
                $page_id = 1;
            }else{
                $page_id = $params['page_id'];
            }

            if($page_id)
            {
                $mod_tables->page_id = $page_id;
                $this->view->page_id = $page_id;
            }
            if($params['date_start'])
            {
                $mod_tables->date_start = $params['date_start'];
                $this->view->date_start = $params['date_start'];
                $params_pool[] ='date_start='.$params['date_start'];
            }
            if($params['date_end'])
            {
                $mod_tables->date_end = $params['date_end'];
                $this->view->date_end = $params['date_end'];
                $params_pool[] = 'date_end='.$params['date_end'];
            }
            if(is_numeric($params['status']))
            {
                $mod_tables->status = $params['status']-1;
                $this->view->status = $params['status'];
                $params_pool[] = 'status='.$params['status']; 
            }
            if($params['keyword'])
            {
                $mod_tables->keyword = $params['keyword'];
                $this->view->keyword = $params['keyword'];
                $params_pool[] = 'keyword='.$params['keyword'];
            }
            $this->view->data = $result = $mod_tables->GetMaozhuaAppIcs();

            //make pagination
            if(!empty($params_pool))
            {
                $params_string = implode("&", $params_pool);
            }else{
                $params_string = "";
            }

            if(1 == $page_id)
            {
                $first_page_string = "";
                $previous_page_string = "";
            }else{
                $first_page_string = "<a href='?page_id=1&".$params_string."'>最前页</a>&nbsp;&nbsp;&nbsp;";
                $previous_page_string = "<a href='?page_id=".($page_id-1)."&".$params_string."'>上一页</a>&nbsp;&nbsp;&nbsp;";
            }

            if($page_id < $result['total_pages'] )
            {
                $next_page_string = "<a href='?page_id=".($page_id+1)."&".$params_string."'>下一页</a>&nbsp;&nbsp;&nbsp;";
                $last_page_string = "<a href='?page_id=".$result['total_pages']."&".$params_string."'>最后页</a>&nbsp;&nbsp;&nbsp;";
            }else{
                $next_page_string = "";
                $last_page_string = "";
            }

            $this->view->params_string = $params_string;
            $this->view->pagination = $first_page_string.$previous_page_string.$next_page_string.$last_page_string;

    //		$select = $maozhua_wb->select();
    //		$select->from("accounts as a", array());
    //		$select->joinLeft("users as u", "u.user_id=a.user_id", array("user_name"));
    //		$select->joinLeft("t_icsapp_login_log as l", "l.userId=u.user_id", array("loginTime", "msg"));
    //		$select->where("a.status = ?", 1);
    //		$select->where("u.status = ?", 1);
    //		$select->where("a.account_type = ?", 4);
    //		$select->where("l.loginTime >= ?", date("Y-m-d")." 00:00:00");
    //
    //		$rows = $maozhua_wb->fetchAll($select);
    //
    //		$this->view->data = $rows;
	}
	
	function appYcAction()
	{
		$params = $this->_request->getParams();
		
		$this->view->title = "易车APP状态";
		
		$maozhua_wb = Zend_Registry::get('maozhua_wb');

		$select = $maozhua_wb->select();
		$select->from("accounts as a", array());
		$select->joinLeft("users as u", "u.user_id=a.user_id", array("user_name"));
		$select->joinLeft("t_ycsapp_login_log as l", "l.userId=u.user_id", array("loginTime", "msg"));
		$select->where("a.status = ?", 1);
		$select->where("u.status = ?", 1);
		$select->where("a.account_type = ?", 5);
                
                if($params['date_start'] or $params['date_end'])
                {
                    if($params['date_start'])
                    {
                        $this->view->date_start = $params['date_start'];
                        $select->where("l.loginTime >= ?",$params['date_start']." 00:00:00");
                    }
                    if($params['date_end'])
                    {
                        $this->view->date_end = $params['date_end'];
                        $select->where("l.loginTime <= ?",$params['date_end']." 23:59:59");
                    }
                }else{
                    $select->where("l.loginTime >= ?", date("Y-m-d")." 00:00:00");
                }
                
                if($params['status'])
                {
                    $this->view->status = $params['status'];
                    $select->where("l.state = ?",$params['status']-1 );
                }
                if($params['keyword'])
                {
                    $keyword = $params['keyword'];
                    $this->view->keyword = $params['keyword'];
                    $select->where("u.user_name like '%$keyword%'");
                }

		$rows = $maozhua_wb->fetchAll($select);
		
		$this->view->data = $rows;
	}
	
        function taskLogsAction()
        {
            $params = $this->_request->getParams();

            $this->view->title = "汽车之家任务执行结果";

            $params_pool = array();
            $mod_tables = new Db_Joins_TablesJoins();

            if($params['date_start'])
            {
                $mod_tables->date_start = $params['date_start'];
                $this->view->date_start = $params['date_start'];
                $params_pool[] = "date_start=".$params['date_start'];
            }

            if($params['date_end'])
            {
                $mod_tables->date_end = $params['date_end'];
                $this->view->date_end = $params['date_end'];
                $params_pool[] = "date_end=".$params['date_end'];
            }

            if($params['keyword'])
            {
                $mod_tables->keyword = $params['keyword'];
                $this->view->keyword = $params['keyword'];
                $params_pool[] = "keyword=".$params['keyword'];
            }
            if($params['status'])
            {
                $mod_tables->status = $params['status']-1;
                $this->view->status = $params['status'];
                $params_pool[] = "status=".$params['status'];
            }
            if(!$params['page_id'])
            {
                $page_id = 1;
            }else{
                $page_id = $params['page_id'];
            }

            if($page_id)
            {
                $mod_tables->page_id = $page_id;
                $this->view->page_id = $page_id;
            }

            $this->view->data = $result = $mod_tables->GetMaozhuaTaskLogs();

            //make pagination
            if(!empty($params_pool))
            {
                $params_string = implode("&", $params_pool);
            }else{
                $params_string = "";
            }

            if(1 == $page_id)
            {
                $first_page_string = "";
                $previous_page_string = "";
            }else{
                $first_page_string = "<a href='?page_id=1&".$params_string."'>最前页</a>&nbsp;&nbsp;&nbsp;";
                $previous_page_string = "<a href='?page_id=".($page_id-1)."&".$params_string."'>上一页</a>&nbsp;&nbsp;&nbsp;";
            }

            if($page_id < $result['total_pages'] )
            {
                $next_page_string = "<a href='?page_id=".($page_id+1)."&".$params_string."'>下一页</a>&nbsp;&nbsp;&nbsp;";
                $last_page_string = "<a href='?page_id=".$result['total_pages']."&".$params_string."'>最后页</a>&nbsp;&nbsp;&nbsp;";
            }else{
                $next_page_string = "";
                $last_page_string = "";
            }

            $this->view->params_string = $params_string;
            $this->view->pagination = $first_page_string.$previous_page_string.$next_page_string.$last_page_string;

        }
	
	function taskLogs2Action()
	{
		$params = $this->_request->getParams();
		
		$this->view->title = "易车任务执行结果";

        $params_pool = array();
        $mod_tables = new Db_Joins_TablesJoins();

        if($params['date_start'])
        {
            $mod_tables->date_start = $params['date_start'];
            $this->view->date_start = $params['date_start'];
            $params_pool[] = "date_start=".$params['date_start'];
        }

        if($params['date_end'])
        {
            $mod_tables->date_end = $params['date_end'];
            $this->view->date_end = $params['date_end'];
            $params_pool[] = "date_end=".$params['date_end'];
        }

        if($params['keyword'])
        {
            $mod_tables->keyword = $params['keyword'];
            $this->view->keyword = $params['keyword'];
            $params_pool[] = "keyword=".$params['keyword'];
        }

        if($params['status'])
        {
            $mod_tables->status = $params['status']-1;
            $this->view->status = $params['status'];
            $params_pool[] = "status=".$params['status'];
            
        }
        if(!$params['page_id'])
        {
            $page_id = 1;
        }else{
            $page_id = $params['page_id'];
        }

        if($page_id)
        {
            $mod_tables->page_id = $page_id;
            $this->view->page_id = $page_id;
        }
        $this->view->data = $result = $mod_tables->GetMaozhuaTaskLogs2();

        //make pagination
        if(!empty($params_pool))
        {
            $params_string = implode("&", $params_pool);
        }else{
            $params_string = "";
        }

        if(1 == $page_id)
        {
            $first_page_string = "";
            $previous_page_string = "";
        }else{
            $first_page_string = "<a href='?page_id=1&".$params_string."'>最前页</a>&nbsp;&nbsp;&nbsp;";
            $previous_page_string = "<a href='?page_id=".($page_id-1)."&".$params_string."'>上一页</a>&nbsp;&nbsp;&nbsp;";
        }

        if($page_id < $result['total_pages'] )
        {
            $next_page_string = "<a href='?page_id=".($page_id+1)."&".$params_string."'>下一页</a>&nbsp;&nbsp;&nbsp;";
            $last_page_string = "<a href='?page_id=".$result['total_pages']."&".$params_string."'>最后页</a>&nbsp;&nbsp;&nbsp;";
        }else{
            $next_page_string = "";
            $last_page_string = "";
        }

        $this->view->params_string = $params_string;
        $this->view->pagination = $first_page_string.$previous_page_string.$next_page_string.$last_page_string;

	}

	function logsAction(){
        $params = $this->_request->getParams();

		$this->view->title = "系统日志查询";

        $params_pool = array();
        $mod_tables = new Db_Joins_TablesJoins();

        if($params['date_start'])
        {
            $mod_tables->date_start = $params['date_start'];
            $this->view->date_start = $params['date_start'];
            $params_pool[] = "date_start=".$params['date_start'];
        }

        if($params['date_end'])
        {
            $mod_tables->date_end = $params['date_end'];
            $this->view->date_end = $params['date_end'];
            $params_pool[] = "date_end=".$params['date_end'];
        }

        if($params['keyword'])
        {
            $mod_tables->keyword = $params['keyword'];
            $this->view->keyword = $params['keyword'];
            $params_pool[] = "keyword=".$params['keyword'];
        }

        if(!$params['page_id'])
        {
            $page_id = 1;
        }else{
            $page_id = $params['page_id'];
        }

        if($page_id)
        {
            $mod_tables->page_id = $page_id;
            $this->view->page_id = $page_id;
        }

        $this->view->data = $result = $mod_tables->GetMaozhualogs();

        //make pagination
        if(!empty($params_pool))
        {
            $params_string = implode("&", $params_pool);
        }else{
            $params_string = "";
        }

        if(1 == $page_id)
        {
            $first_page_string = "";
            $previous_page_string = "";
        }else{
            $first_page_string = "<a href='?page_id=1&".$params_string."'>最前页</a>&nbsp;&nbsp;&nbsp;";
            $previous_page_string = "<a href='?page_id=".($page_id-1)."&".$params_string."'>上一页</a>&nbsp;&nbsp;&nbsp;";
        }

        if($page_id < $result['total_pages'] )
        {
            $next_page_string = "<a href='?page_id=".($page_id+1)."&".$params_string."'>下一页</a>&nbsp;&nbsp;&nbsp;";
            $last_page_string = "<a href='?page_id=".$result['total_pages']."&".$params_string."'>最后页</a>&nbsp;&nbsp;&nbsp;";
        }else{
            $next_page_string = "";
            $last_page_string = "";
        }

        $this->view->params_string = $params_string;
        $this->view->pagination = $first_page_string.$previous_page_string.$next_page_string.$last_page_string;
    }
	
	function indexDataAction()
	{
            $params = $this->_request->getParams();
            $this->view->title = "工作台数据查询";

            $params_pool = array();
            $mod_tables = new Db_Joins_TablesJoins();

            if($params['date_start'])
            {
                $mod_tables->date_start = $params['date_start'];
                $this->view->date_start = $params['date_start'];
                $params_pool[] = "date_start=".$params['date_start'];
            }

            if($params['date_end'])
            {
                $mod_tables->date_end = $params['date_end'];
                $this->view->date_end = $params['date_end'];
                $params_pool[] = "date_end=".$params['date_end'];
            }

            if($params['keyword'])
            {
                $mod_tables->keyword = $params['keyword'];
                $this->view->keyword = $params['keyword'];
                $params_pool[] = "keyword=".$params['keyword'];
            }
            
            if($params['status_zj'])
            {
                $mod_tables->status_zj = $params['status_zj']-1;
                $this->view->status_zj = $params['status_zj'];
                $params_pool[] = "status_zj=".$params['status_zj'];
            }
            
            if($params['zj_manfen'])
            {
                $mod_tables->zj_manfen = $params['zj_manfen']-1;
                $this->view->zj_manfen = $params['zj_manfen'];
                $params_pool[] = "zj_manfen=".$params['zj_manfen'];
            }
            
            if($params['status_yc'])
            {
                $mod_tables->status_yc = $params['status_yc']-1;
                $this->view->status_yc = $params['status_yc'];
                $params_pool[] = "status_yc=".$params['status_yc'];
            }
            if($params['yc_manfen'])
            {
                $mod_tables->yc_manfen = $params['yc_manfen']-1;
                $this->view->yc_manfen = $params['yc_manfen'];
                $params_pool[] = "yc_manfen=".$params['yc_manfen'];
            }
            
            if(!$params['page_id'])
            {
                $page_id = 1;
            }else{
                $page_id = $params['page_id'];
            }

            if($page_id)
            {
                $mod_tables->page_id = $page_id;
                $this->view->page_id = $page_id;
            }

            $this->view->data = $result = $mod_tables->GetMaozhuaIndexData(0);

            //make pagination
            if(!empty($params_pool))
            {
                $params_string = implode("&", $params_pool);
            }else{
                $params_string = "";
            }

            if(1 == $page_id)
            {
                $first_page_string = "";
                $previous_page_string = "";
            }else{
                $first_page_string = "<a href='?page_id=1&".$params_string."'>最前页</a>&nbsp;&nbsp;&nbsp;";
                $previous_page_string = "<a href='?page_id=".($page_id-1)."&".$params_string."'>上一页</a>&nbsp;&nbsp;&nbsp;";
            }

            if($page_id < $result['total_pages'] )
            {
                $next_page_string = "<a href='?page_id=".($page_id+1)."&".$params_string."'>下一页</a>&nbsp;&nbsp;&nbsp;";
                $last_page_string = "<a href='?page_id=".$result['total_pages']."&".$params_string."'>最后页</a>&nbsp;&nbsp;&nbsp;";
            }else{
                $next_page_string = "";
                $last_page_string = "";
            }

            $this->view->params_string = $params_string;
            $this->view->pagination = $first_page_string.$previous_page_string.$next_page_string.$last_page_string;


    //        var_dump($result);die;

    //		$keyword = "";
    //		$time_start = "";
    //		$time_end = "";
    //
    //		if($params['keyword'])
    //		{
    //			$keyword = trim($params['keyword']);
    //			$this->view->keyword = $keyword;
    //		}
    //
    //		if($params['time_start'])
    //		{
    //			$time_start = trim($params['time_start']);
    //			$this->view->time_start = $time_start;
    //		}
    //
    //		if($params['time_end'])
    //		{
    //			$time_end = trim($params['time_end']);
    //			$this->view->time_end = $time_end;
    //		}
    //
    //		$maozhua_wb = Zend_Registry::get('maozhua_wb');
    //
    //		$select = $maozhua_wb->select();
    //		$select->from("logs_index_data as l", array("log_id", "status_zj", "status_yc", "data_zj", "data_yc", "ctime"));
    //		$select->joinLeft("users as u", "u.user_id=l.user_id", array("user_name"));
    //
    //		if($keyword)
    //		{
    //			$select->where("u.user_name like '%".$keyword."%' or u.cellphone like '%".$keyword."%'");
    //		}
    //
    //		if($time_start)
    //		{
    //			$select->where("l.ctime >= ?", $time_start." 00:00:00");
    //		}
    //
    //		if($time_end)
    //		{
    //			$select->where("l.ctime <= ?", $time_end." 23:59:59");
    //		}
    //
    //		$select->order("l.log_id desc");
    //		$select->limit(100);
    //
    //		$rows = $maozhua_wb->fetchAll($select);
    //
    //		$this->view->data = $rows;
	}
	
	 public function exportIndexDataAction()
    {
        $params = $this->_request->getParams();

        $mod_tables = new Db_Joins_TablesJoins();
		
		if($params['date_start'])
		{
			$mod_tables->date_start = $params['date_start'];
			$this->view->date_start = $params['date_start'];
		}

		if($params['date_end'])
		{
			$mod_tables->date_end = $params['date_end'];
			$this->view->date_end = $params['date_end'];
		}

		if($params['keyword'])
		{
			$mod_tables->keyword = $params['keyword'];
			$this->view->keyword = $params['keyword'];
		}

		if($params['status_zj'])
		{
			$mod_tables->status_zj = $params['status_zj']-1;
			$this->view->status_zj = $params['status_zj'];
		}

		if($params['zj_manfen'])
		{
			$mod_tables->zj_manfen = $params['zj_manfen']-1;
			$this->view->zj_manfen = $params['zj_manfen'];
		}

		if($params['status_yc'])
		{
			$mod_tables->status_yc = $params['status_yc']-1;
			$this->view->status_yc = $params['status_yc'];
		}
		if($params['yc_manfen'])
		{
			$mod_tables->yc_manfen = $params['yc_manfen']-1;
			$this->view->yc_manfen = $params['yc_manfen'];
		}
        
        $result = $mod_tables->GetMaozhuaIndexData(1);
		
        $objPHPExcel = new PHPExcel();

		if (!empty($result['current_page_info'])) {

			$objPHPExcel->setActiveSheetIndex(0)->setCellValue('A1', 'ID');
			$objPHPExcel->setActiveSheetIndex(0)->setCellValue('B1', '公司名称');
			$objPHPExcel->setActiveSheetIndex(0)->setCellValue('C1', '记录时间');
			$objPHPExcel->setActiveSheetIndex(0)->setCellValue('D1', '之家状态');
			$objPHPExcel->setActiveSheetIndex(0)->setCellValue('E1', '之家积分');
			$objPHPExcel->setActiveSheetIndex(0)->setCellValue('F1', '之家满分');
			$objPHPExcel->setActiveSheetIndex(0)->setCellValue('G1', '之家推荐名额(已使用/总额度)');
			$objPHPExcel->setActiveSheetIndex(0)->setCellValue('H1', '之家文章覆盖');
			$objPHPExcel->setActiveSheetIndex(0)->setCellValue('I1', '易车状态');
			$objPHPExcel->setActiveSheetIndex(0)->setCellValue('J1', '易车积分');
			$objPHPExcel->setActiveSheetIndex(0)->setCellValue('K1', '易车满分');
			$objPHPExcel->setActiveSheetIndex(0)->setCellValue('L1', '易车降价文章覆盖度');
			$objPHPExcel->setActiveSheetIndex(0)->setCellValue('M1', '易车置换文章覆盖度');

			$n = 1;
			foreach($result['current_page_info']['log_id'] as $v)
			{
				$n++;
				$objPHPExcel->setActiveSheetIndex(0)->setCellValue('A' . $n, $v);
			}
			$n = 1;
			foreach($result['current_page_info']['user_name'] as $v)
			{
				$n++;
				$objPHPExcel->setActiveSheetIndex(0)->setCellValue('B' . $n, $v);
			}
			$n = 1;
			foreach($result['current_page_info']['ctime'] as $v)
			{
				$n++;
				$objPHPExcel->setActiveSheetIndex(0)->setCellValue('C' . $n, $v);
			}
			$n = 1;
			foreach($result['current_page_info']['status_zj'] as $v)
			{
				$n++;
				$objPHPExcel->setActiveSheetIndex(0)->setCellValue('D' . $n, $v ? "正常" : "下线");
			}
			$n = 1;
			foreach($result['current_page_info']['status_yc'] as $v)
			{
				$n++;
				$objPHPExcel->setActiveSheetIndex(0)->setCellValue('I' . $n, $v ? "正常" : "下线");
			}

			$n = 1;
			$data = $result['current_page_info'];
			foreach($data["log_id"] as $id => $something){

				$zj_score = "";
				$zj_manfen = "";
				$zj_minger = "";
				$zj_fugai = "";

				if($data['status_zj'][$id])
				{
					$data_zj = Zend_Json::decode($data['data_zj'][$id]);

					$zj_score_arr = array();

					if($data_zj['data']['yunying'])
					{
						foreach($data_zj['data']['yunying'] as $fdata)
						{
							$zj_score_arr[] = $fdata['totalScore'];
						}

						$zj_score = implode("/", $zj_score_arr);
					}

					$zj_manfen = $data_zj['data']['manfen']?"是":"否";                

					$haofei = $data_zj['data']['haofei']?$data_zj['data']['haofei']:0;
					$zongedu = ($data_zj['data']['haofei']+$data_zj['data']['shengyu'])?($data_zj['data']['haofei']+$data_zj['data']['shengyu']):0;

					$zj_minger = $haofei."/".$zongedu;

					$series1 = 0; //有文章
					$series2 = 0; //总数

					if($data_zj['data']['series'])
					{
						foreach($data_zj['data']['series'] as $se)
						{
							$series2 += 1;

							$str = str_replace("）", "", $se['_name']);
							$ex = explode("（", $str);

							if(2 == count($ex))
							{
								if(0 < $ex[1])
								{
									$series1 += 1;
								}
							}
						}
					}

					$zj_fugai = $series1."/".$series2;

					if($series1 != $series2)
					{
						$zj_fugai = "<font color='red'>".$zj_fugai."</font>";
					}
				}

				$yc_score = "";
				$yc_manfen = "";
				$yc_topic1 = "";
				$yc_topic2 = "";

				if($data['status_yc'][$id])
				{
					$data_yc = Zend_Json::decode($data['data_yc'][$id]);

					$yc_score_arr = array();

					if($data_yc['data']['yunying'])
					{
						foreach($data_yc['data']['yunying'] as $fdata)
						{
							$yc_score_arr[] = $fdata['totalScore'];
						}

						$yc_score = implode("/", $yc_score_arr);
					}

					$yc_manfen = $data_yc['data']['isScore100']?"是":"否";
					$yc_topic1 = $data_yc['data']['jiangjiaNewsCover']?$data_yc['data']['jiangjiaNewsCover']:"-";
					$yc_topic2 = $data_yc['data']['zhihuanNewsCover']?$data_yc['data']['zhihuanNewsCover']:"-";

				}
				$n++;
				$objPHPExcel->setActiveSheetIndex(0)->setCellValue('E' . $n, $zj_score);
				$objPHPExcel->setActiveSheetIndex(0)->setCellValue('F' . $n, $zj_manfen);
				$objPHPExcel->setActiveSheetIndex(0)->setCellValue('G' . $n, $zj_minger);
				$objPHPExcel->setActiveSheetIndex(0)->setCellValue('H' . $n, $zj_fugai);
				$objPHPExcel->setActiveSheetIndex(0)->setCellValue('J' . $n, $yc_score);
				$objPHPExcel->setActiveSheetIndex(0)->setCellValue('K' . $n, $yc_manfen);
				$objPHPExcel->setActiveSheetIndex(0)->setCellValue('L' . $n, $yc_topic1);
				$objPHPExcel->setActiveSheetIndex(0)->setCellValue('M' . $n, $yc_topic2);
			}
		}

		$rd = mt_rand(1000, 9999);
		$folder = "upload/reports/";
		$file_name = "reports-index-data-" . date("YmdHis") . $rd . ".xlsx";
		$objWriter = PHPExcel_IOFactory::createWriter($objPHPExcel, 'Excel2007');
		$objWriter->save($folder . $file_name);

		header("Location: /" . $folder . $file_name);

		echo "End.";
		die;

    }
	

	function indexDataZjAction()
	{
            $params = $this->_request->getParams();
            $this->view->title = "汽车之家工作台";
				
            $params_pool = array();
            $mod_tables = new Db_Joins_TablesJoins();

            if($params['date_start'])
            {
                $mod_tables->date_start = $params['date_start'];
                $this->view->date_start = $params['date_start'];
                $params_pool[] = "date_start=".$params['date_start'];
            }

            if($params['date_end'])
            {
                $mod_tables->date_end = $params['date_end'];
                $this->view->date_end = $params['date_end'];
                $params_pool[] = "date_end=".$params['date_end'];
            }

            if($params['keyword'])
            {
                $mod_tables->keyword = $params['keyword'];
                $this->view->keyword = $params['keyword'];
                $params_pool[] = "keyword=".$params['keyword'];
            }
            
            if($params['status_zj'])
            {
                $mod_tables->status_zj = $params['status_zj']-1;
                $this->view->status_zj = $params['status_zj'];
                $params_pool[] = "status_zj=".$params['status_zj'];
            }
			
			if(is_numeric($params['account_status']))
            {
                $mod_tables->account_status = $params['account_status'];
                $this->view->account_status = $params['account_status'];
                $params_pool[] = "account_status=".$params['account_status'];
            }
            
            if($params['zj_manfen'])
            {
                $mod_tables->zj_manfen = $params['zj_manfen']-1;
                $this->view->zj_manfen = $params['zj_manfen'];
                $params_pool[] = "zj_manfen=".$params['zj_manfen'];
            }
            
            if($params['status_yc'])
            {
                $mod_tables->status_yc = $params['status_yc']-1;
                $this->view->status_yc = $params['status_yc'];
                $params_pool[] = "status_yc=".$params['status_yc'];
            }
            if($params['yc_manfen'])
            {
                $mod_tables->yc_manfen = $params['yc_manfen']-1;
                $this->view->yc_manfen = $params['yc_manfen'];
                $params_pool[] = "yc_manfen=".$params['yc_manfen'];
            }
            
            if(!$params['page_id'])
            {
                $page_id = 1;
            }else{
                $page_id = $params['page_id'];
            }

            if($page_id)
            {
                $mod_tables->page_id = $page_id;
                $this->view->page_id = $page_id;
            }

            $this->view->data = $result = $mod_tables->GetMaozhuaIndexZjData(1);

            //make pagination
            if(!empty($params_pool))
            {
                $params_string = implode("&", $params_pool);
            }else{
                $params_string = "";
            }

            if(1 == $page_id)
            {
                $first_page_string = "";
                $previous_page_string = "";
            }else{
                $first_page_string = "<a href='?page_id=1&".$params_string."'>最前页</a>&nbsp;&nbsp;&nbsp;";
                $previous_page_string = "<a href='?page_id=".($page_id-1)."&".$params_string."'>上一页</a>&nbsp;&nbsp;&nbsp;";
            }

            if($page_id < $result['total_pages'] )
            {
                $next_page_string = "<a href='?page_id=".($page_id+1)."&".$params_string."'>下一页</a>&nbsp;&nbsp;&nbsp;";
                $last_page_string = "<a href='?page_id=".$result['total_pages']."&".$params_string."'>最后页</a>&nbsp;&nbsp;&nbsp;";
            }else{
                $next_page_string = "";
                $last_page_string = "";
            }

            $this->view->params_string = $params_string;
            $this->view->pagination = $first_page_string.$previous_page_string.$next_page_string.$last_page_string;


    //        var_dump($result);die;

    //		$keyword = "";
    //		$time_start = "";
    //		$time_end = "";
    //
    //		if($params['keyword'])
    //		{
    //			$keyword = trim($params['keyword']);
    //			$this->view->keyword = $keyword;
    //		}
    //
    //		if($params['time_start'])
    //		{
    //			$time_start = trim($params['time_start']);
    //			$this->view->time_start = $time_start;
    //		}
    //
    //		if($params['time_end'])
    //		{
    //			$time_end = trim($params['time_end']);
    //			$this->view->time_end = $time_end;
    //		}
    //
    //		$maozhua_wb = Zend_Registry::get('maozhua_wb');
    //
    //		$select = $maozhua_wb->select();
    //		$select->from("logs_index_data as l", array("log_id", "status_zj", "status_yc", "data_zj", "data_yc", "ctime"));
    //		$select->joinLeft("users as u", "u.user_id=l.user_id", array("user_name"));
    //
    //		if($keyword)
    //		{
    //			$select->where("u.user_name like '%".$keyword."%' or u.cellphone like '%".$keyword."%'");
    //		}
    //
    //		if($time_start)
    //		{
    //			$select->where("l.ctime >= ?", $time_start." 00:00:00");
    //		}
    //
    //		if($time_end)
    //		{
    //			$select->where("l.ctime <= ?", $time_end." 23:59:59");
    //		}
    //
    //		$select->order("l.log_id desc");
    //		$select->limit(100);
    //
    //		$rows = $maozhua_wb->fetchAll($select);
    //
    //		$this->view->data = $rows;
	}
	
	function indexDataYcAction()
	{
            $params = $this->_request->getParams();
            $this->view->title = "易车工作台";

            $params_pool = array();
            $mod_tables = new Db_Joins_TablesJoins();

            if($params['date_start'])
            {
                $mod_tables->date_start = $params['date_start'];
                $this->view->date_start = $params['date_start'];
                $params_pool[] = "date_start=".$params['date_start'];
            }

            if($params['date_end'])
            {
                $mod_tables->date_end = $params['date_end'];
                $this->view->date_end = $params['date_end'];
                $params_pool[] = "date_end=".$params['date_end'];
            }

            if($params['keyword'])
            {
                $mod_tables->keyword = $params['keyword'];
                $this->view->keyword = $params['keyword'];
                $params_pool[] = "keyword=".$params['keyword'];
            }
            
            if($params['status_zj'])
            {
                $mod_tables->status_zj = $params['status_zj']-1;
                $this->view->status_zj = $params['status_zj'];
                $params_pool[] = "status_zj=".$params['status_zj'];
            }
            
            if($params['zj_manfen'])
            {
                $mod_tables->zj_manfen = $params['zj_manfen']-1;
                $this->view->zj_manfen = $params['zj_manfen'];
                $params_pool[] = "zj_manfen=".$params['zj_manfen'];
            }
            
            if($params['status_yc'])
            {
                $mod_tables->status_yc = $params['status_yc']-1;
                $this->view->status_yc = $params['status_yc'];
                $params_pool[] = "status_yc=".$params['status_yc'];
            }
			
			if(is_numeric($params['account_status']))
            {
                $mod_tables->account_status = $params['account_status'];
                $this->view->account_status = $params['account_status'];
                $params_pool[] = "account_status=".$params['account_status'];
            }
			
            if($params['yc_manfen'])
            {
                $mod_tables->yc_manfen = $params['yc_manfen']-1;
                $this->view->yc_manfen = $params['yc_manfen'];
                $params_pool[] = "yc_manfen=".$params['yc_manfen'];
            }
            
            if(!$params['page_id'])
            {
                $page_id = 1;
            }else{
                $page_id = $params['page_id'];
            }

            if($page_id)
            {
                $mod_tables->page_id = $page_id;
                $this->view->page_id = $page_id;
            }

            $this->view->data = $result = $mod_tables->GetMaozhuaIndexZjData(2);

            //make pagination
            if(!empty($params_pool))
            {
                $params_string = implode("&", $params_pool);
            }else{
                $params_string = "";
            }

            if(1 == $page_id)
            {
                $first_page_string = "";
                $previous_page_string = "";
            }else{
                $first_page_string = "<a href='?page_id=1&".$params_string."'>最前页</a>&nbsp;&nbsp;&nbsp;";
                $previous_page_string = "<a href='?page_id=".($page_id-1)."&".$params_string."'>上一页</a>&nbsp;&nbsp;&nbsp;";
            }

            if($page_id < $result['total_pages'] )
            {
                $next_page_string = "<a href='?page_id=".($page_id+1)."&".$params_string."'>下一页</a>&nbsp;&nbsp;&nbsp;";
                $last_page_string = "<a href='?page_id=".$result['total_pages']."&".$params_string."'>最后页</a>&nbsp;&nbsp;&nbsp;";
            }else{
                $next_page_string = "";
                $last_page_string = "";
            }

            $this->view->params_string = $params_string;
            $this->view->pagination = $first_page_string.$previous_page_string.$next_page_string.$last_page_string;

	}
	
	function mpLogsAction()
    {
        $params = $this->_request->getParams();
        //$this->view->title = "消息盒子";
        $maozhua_wb = Zend_Registry::get('maozhua_wb');
        if(!$params['page_id'])
        {
            $page_id = 1;
        }else{
            $page_id = $params['page_id'];
        }

        $size = 100;
        $select = $maozhua_wb->select();
        $select->from("mp_logs as m",array("count(*) as total"));
        $select->joinLeft("users as u","m.user_id=u.user_id",array("user_name"));
        if($params['date_start'])
        {
            $select->where("m.ctime > ?",$params['date_start']." 00:00:00");
            $this->view->date_start = $params['date_start'];
        }
        if($params['date_end'])
        {
            $select->where("m.ctime < ?",$params['date_end']." 23:59:59");
            $this->view->date_end = $params['date_end'];
        }
        if($params['keyword'])
        {
            $keyword = $params['keyword'];
            $select->where("m.content like '%$keyword%' or u.user_name like '%$keyword%'");
            $this->view->keyword = $keyword;
        }
        $result = $maozhua_wb->fetchRow($select);
        $result['total_pages'] = $result['total']/$size;

        $select = $maozhua_wb->select();
        $select->from("mp_logs as m",array("*"));
        $select->joinLeft("users as u","m.user_id=u.user_id",array("user_name"));
        if($params['date_start'])
        {
            $select->where("m.ctime > ?",$params['date_start']." 00:00:00");
            $params_pool[] = "date_start=".$params['date_start'];
        }
        if($params['date_end'])
        {
            $select->where("m.ctime < ?",$params['date_end']." 23:59:59");
            $params_pool[] = "date_end=".$params['date_end'];
        }
        if($params['keyword'])
        {
            $keyword = $params['keyword'];
            $select->where("m.content like '%$keyword%' or u.user_name like '%$keyword%'");
            $this->view->keyword = $keyword;
            $params_pool[] = "keyword=".$params['keyword'];
        }
        $select->order("m.ctime DESC");
        $select->limit($size, $size * ($page_id - 1));
        $data = $maozhua_wb->fetchAll($select);

        if($page_id)
        {
            $this->view->page_id = $page_id;
        }
        if(!empty($params_pool))
        {
            $params_string = implode("&", $params_pool);
        }else{
            $params_string = "";
        }



        if(1 == $page_id)
        {
            $first_page_string = "";
            $previous_page_string = "";
        }else{
            $first_page_string = "<a href='?page_id=1&".$params_string."'>最前页</a>&nbsp;&nbsp;&nbsp;";
            $previous_page_string = "<a href='?page_id=".($page_id-1)."&".$params_string."'>上一页</a>&nbsp;&nbsp;&nbsp;";
        }
        if($page_id < $result['total_pages'] )
        {
            $next_page_string = "<a href='?page_id=".($page_id+1)."&".$params_string."'>下一页</a>&nbsp;&nbsp;&nbsp;";
            $last_page_string = "<a href='?page_id=".$result['total_pages']."&".$params_string."'>最后页</a>&nbsp;&nbsp;&nbsp;";
        }else{
            $next_page_string = "";
            $last_page_string = "";
        }
        $this->view->pagination = $first_page_string.$previous_page_string.$next_page_string.$last_page_string;
        $this->view->data = $data;
    }
    
    
    
    function userEmailAction()
    {
        $maozhua_wb = Zend_Registry::get('maozhua_wb');
        $params = $this->_request->getParams();
        if(!$params['page_id'])
        {
            $page_id = 1;
        }else{
            $page_id = $params['page_id'];
        }
        $size = 50;

        $select = $maozhua_wb->select();
        $select->from("user_email",array("*"));
        if($params['keyword'])
        {
            $keyword = $params['keyword'];
            $select->where("user_name like '%$keyword%'");
            $this->view->keyword = $keyword;
            $params_pool[] = "keyword=".$params['keyword'];
        }
        if($params['status'])
        {
            $select->where("status in (?)",$params['status']);
            $this->view->status_arr = $params['status'];
            foreach($params['status'] as $status){
                $params_pool[] = "status[]=".$status;
            }

        }
        $select->order("id desc");
        $select->limit($size, $size * ($page_id - 1));
        $data = $maozhua_wb->fetchAll($select);

        $select = $maozhua_wb->select();
        $select->from("user_email",array("count(*) as total_pages"));
        if($params['keyword'])
        {
            $select->where("user_name like '%$keyword%'");
        }
        if($params['status'])
        {
            $select->where("status in (?)",$params['status']);
        }
        $result = $maozhua_wb->fetchRow($select);

        if($page_id)
        {
            $this->view->page_id = $page_id;
        }
        if(!empty($params_pool))
        {
            $params_string = implode("&", $params_pool);
        }else{
            $params_string = "";
        }
        if(1 == $page_id)
        {
            $first_page_string = "";
            $previous_page_string = "";
        }else{
            $first_page_string = "<a href='?page_id=1&".$params_string."'>最前页</a>&nbsp;&nbsp;&nbsp;";
            $previous_page_string = "<a href='?page_id=".($page_id-1)."&".$params_string."'>上一页</a>&nbsp;&nbsp;&nbsp;";
        }
        if($page_id < $result['total_pages'] )
        {
            $next_page_string = "<a href='?page_id=".($page_id+1)."&".$params_string."'>下一页</a>&nbsp;&nbsp;&nbsp;";
            $last_page_string = "<a href='?page_id=".$result['total_pages']."&".$params_string."'>最后页</a>&nbsp;&nbsp;&nbsp;";
        }else{
            $next_page_string = "";
            $last_page_string = "";
        }
        $this->view->pagination = $first_page_string.$previous_page_string.$next_page_string.$last_page_string;
        $this->view->data = $data;
    }

    function addUserEmailAction()
    {
        $this->view->title = "新增邮箱";
    }

    function addUserEmailSubmitAction()
    {
        $maozhua_wb = Zend_Registry::get('maozhua_wb');
        $params = $this->_request->getParams();
        $set = array(
            "user_name"=>$params['user_name'],
            "email"=>$params['email'],
            "ctime"=>date("Y-m-d H:i:s"),
            "status"=>$params['status']
        );
        $maozhua_wb->insert("user_email",$set);
        $this->_redirect("/maozhua/user-email/msg/1");
    }

    function editUserEmailAction()
    {
        $params = $this->_request->getParams();
        $maozhua_wb = Zend_Registry::get('maozhua_wb');
        $select = $maozhua_wb->select();
        $select->from("user_email",array("*"));
        $select->where("id = ?",$params['id']);
        $data = $maozhua_wb->fetchRow($select);
        $this->view->data = $data;
    }

    function editUserEmailSubmitAction()
    {
        $maozhua_wb = Zend_Registry::get('maozhua_wb');
        $params = $this->_request->getParams();
        $update = array(
            "user_name"=>$params['user_name'],
            "email"=>$params['email'],
            "status"=>$params['status']
        );
        $where = "id=".$params['id'];
        $maozhua_wb->update("user_email",$update,$where);
        $this->_redirect("/maozhua/user-email/msg/1");
    }
    
    public function exportUsersAction()
    {
        $params = $this->_request->getParams();

        $params_pool = array();
        $mod_tables = new Db_Joins_TablesJoins();
        
        $result = $mod_tables->GetMaozhuaUsersExport();
        $objPHPExcel = new PHPExcel();

            if (!empty($result)) {

                $objPHPExcel->setActiveSheetIndex(0)->setCellValue('A1', '公司名称');
                $objPHPExcel->setActiveSheetIndex(0)->setCellValue('B1', '省');
                $objPHPExcel->setActiveSheetIndex(0)->setCellValue('C1', '市');
                $objPHPExcel->setActiveSheetIndex(0)->setCellValue('D1', '手机/登录名');
                $objPHPExcel->setActiveSheetIndex(0)->setCellValue('E1', '负责销售');
                $objPHPExcel->setActiveSheetIndex(0)->setCellValue('F1', '起始日');
                $objPHPExcel->setActiveSheetIndex(0)->setCellValue('G1', '到期日');
                $objPHPExcel->setActiveSheetIndex(0)->setCellValue('H1', '状态');
                $objPHPExcel->setActiveSheetIndex(0)->setCellValue('I1', '合同编号');
                $n = 1;
                foreach($result['current_page_info']['user_name'] as $v)
                {
                    $n++;
                    $objPHPExcel->setActiveSheetIndex(0)->setCellValue('A' . $n, $v);
                }
                $n = 1;
                foreach($result['current_page_info']['provinceName'] as $v)
                {
                    $n++;
                    $objPHPExcel->setActiveSheetIndex(0)->setCellValue('B' . $n, $v);
                }
                $n = 1;
                foreach($result['current_page_info']['cityName'] as $v)
                {
                    $n++;
                    $objPHPExcel->setActiveSheetIndex(0)->setCellValue('C' . $n, $v);
                }
                $n = 1;
                foreach($result['current_page_info']['cellphone'] as $v)
                {
                    $n++;
                    $objPHPExcel->setActiveSheetIndex(0)->setCellValue('D' . $n, $v);
                }
                $n = 1;
                foreach($result['current_page_info']['emp_name'] as $v)
                {
                    $n++;
                    $objPHPExcel->setActiveSheetIndex(0)->setCellValue('E' . $n, $v);
                }
                $n = 1;
                foreach($result['current_page_info']['start_date'] as $v)
                {
                    $n++;
                    $objPHPExcel->setActiveSheetIndex(0)->setCellValue('F' . $n, $v);
                }
                $n = 1;
                foreach($result['current_page_info']['expired_date'] as $v)
                {
                    $n++;
                    $objPHPExcel->setActiveSheetIndex(0)->setCellValue('G' . $n, $v);
                }
                $n = 1;
                foreach($result['current_page_info']['status'] as $v)
                {
                    $n++;
                    $objPHPExcel->setActiveSheetIndex(0)->setCellValue('H' . $n, $v ? "已审核" : "未审核");
                   
                }
                $n = 1;
                foreach($result['current_page_info']['contract_sid'] as $v)
                {
                    $n++;
                    $contract = explode(",",$v);
                    $english_arr = array("I","j","k","l","M","N","O","P","Q","R","S","T","U","V","W","X","Y","Z");
                    foreach($contract as $k=>$item)
                    {
                        $objPHPExcel->setActiveSheetIndex(0)->setCellValue($english_arr[$k] . $n, $item);
                    }
                   
                }
            }

            $rd = mt_rand(1000, 9999);
            $folder = "upload/reports/";
            $file_name = "reports-users-" . date("YmdHis") . $rd . ".xlsx";
            $objWriter = PHPExcel_IOFactory::createWriter($objPHPExcel, 'Excel2007');
            $objWriter->save($folder . $file_name);

            header("Location: /" . $folder . $file_name);

            echo "End.";
            die;

    }
	
	

	
	function importExcelPageAction() {
		$params = $this->_request->getParams();
		$maozhua_wb = Zend_Registry::get('maozhua_wb');
		
		$this->view->title = "月度数据报表导入";
		
		if($params['timestamp']){
			$timestamp=$params['timestamp'];
			$year_month=date('Y年n月',$timestamp);
			
			$select = $maozhua_wb->select();
			$select->from("logs_month_remark",array("year_month"));
			$select->where("timestamp = ?",$timestamp);
			$data = $maozhua_wb->fetchRow($select);
			
			if(!$data){
				$set=array(
					'year_month' => $year_month,
					'timestamp' => $timestamp,
				);
				$maozhua_wb->insert("logs_month_remark",$set);
			}
		}
	}
	
	function importExcelAction(){
		set_time_limit(3600);
		$timestamp=strtotime($_POST['year_month'].'-01 00:00:00');
		$year_month=date('Y年n月',$timestamp);
		$ctime=date('Y-m-d 00:00:00',$timestamp);

		
		//判断是否选择了要上传的表格
		if (empty($_FILES['myfile']['name'])) {
			echo "<script>alert('您未选择表格');window.location.href='/maozhua/import-excel-page';</script>";
			die;
		}

		//获取表格的大小，限制上传表格的大小5M
		$file_size = $_FILES['myfile']['size'];
		if ($file_size>8*1024*1024) {
			echo "<script>alert('上传失败，上传的表格不能超过5M的大小');window.location.href='/maozhua/import-excel-page';</script>";
			die;
		}
		
		if (!empty($_FILES['myfile']['name'])) {
			$fileName = $_FILES['myfile']['name'];    //得到文件全名
			$dotArray = explode('.', $fileName);    //把文件名安.区分，拆分成数组
			$type = end($dotArray);
			if ($type != "xls" && $type != "xlsx") {
				echo "<script>alert('上传失败，只能上传Excel文件!');window.location.href='/maozhua/import-excel-page'</script>";
				die;
			}
		}
		
		//判断表格是否上传成功
		if (is_uploaded_file($_FILES['myfile']['tmp_name'])) {
			try {
				$maozhua_wb = Zend_Registry::get('maozhua_wb');
				$objPHPExcel = new PHPExcel();
				$objReader = PHPExcel_IOFactory::createReader('Excel5');

				//接收存在缓存中的excel表格
				$filename = $_FILES['myfile']['tmp_name'];
				$objPHPexcel = $objReader->load($filename); //$filename可以是上传的表格，或者是指定的表格
				$sheet = $objPHPexcel->getSheet(0);
				$highestRow = $sheet->getHighestRow(); // 取得总行数
				// $highestColumn = $sheet->getHighestColumn(); // 取得总列数
			} catch (Exception $exc) {
				echo "<script>alert('读取文件失败！请查看上传文件的版本是否符合要求。');window.location.href='/maozhua/import-excel-page';</script>";
				die;
			}
			//循环读取excel表格,读取一条,插入一条
			//j表示从哪一行开始读取  从第二行开始读取，因为第一行是标题不保存
			for($j=2;$j<=$highestRow;$j++) 
			{
				$a = $objPHPexcel->getActiveSheet()->getCell("A".$j)->getValue();
				$b = $objPHPexcel->getActiveSheet()->getCell("B".$j)->getValue();
				$c = $objPHPexcel->getActiveSheet()->getCell("C".$j)->getValue();
				$d = $objPHPexcel->getActiveSheet()->getCell("D".$j)->getValue();
				$e = $objPHPexcel->getActiveSheet()->getCell("E".$j)->getValue();
				$f = $objPHPexcel->getActiveSheet()->getCell("F".$j)->getValue();
				$g = $objPHPexcel->getActiveSheet()->getCell("G".$j)->getValue();
				$h = $objPHPexcel->getActiveSheet()->getCell("H".$j)->getValue();
				$i = $objPHPexcel->getActiveSheet()->getCell("I".$j)->getValue();
				$jj = $objPHPexcel->getActiveSheet()->getCell("J".$j)->getValue();
				$k = $objPHPexcel->getActiveSheet()->getCell("K".$j)->getValue();
				$l = $objPHPexcel->getActiveSheet()->getCell("L".$j)->getValue();
				$m = $objPHPexcel->getActiveSheet()->getCell("M".$j)->getValue();
				$n = $objPHPexcel->getActiveSheet()->getCell("N".$j)->getValue();
				$o = $objPHPexcel->getActiveSheet()->getCell("O".$j)->getValue();
				$p = $objPHPexcel->getActiveSheet()->getCell("P".$j)->getValue();
				$q = $objPHPexcel->getActiveSheet()->getCell("Q".$j)->getValue();
				$r = $objPHPexcel->getActiveSheet()->getCell("R".$j)->getValue();
				$s = $objPHPexcel->getActiveSheet()->getCell("S".$j)->getValue();
				$t = $objPHPexcel->getActiveSheet()->getCell("T".$j)->getValue();
				$u = $objPHPexcel->getActiveSheet()->getCell("U".$j)->getValue();
				$v = $objPHPexcel->getActiveSheet()->getCell("V".$j)->getValue();
				$w = $objPHPexcel->getActiveSheet()->getCell("W".$j)->getValue();
				$x = $objPHPexcel->getActiveSheet()->getCell("X".$j)->getValue();
				$set = array(
					"dealer_name"=>$a,
					"group_name"=>$b,
					"group_id"=>$c,
					"dealer_id"=>$d,
					"province"=>$e,
					"city"=>$f,
					"factory_name"=>$g,
					"recommend_places_score"=>$h,
					"valid_carseries_article_score"=>$i,
					"pick_up_400_score"=>$jj,
					"dealer_engine_total_points"=>$k,
					"private_clues"=>$o,
					"csh_allot_sale_cules"=>$p,
					"csh_loot_public_orders"=>$q,
					"csh_400_public_number"=>$r,
					"product_library_score"=>$s,
					"csh_pv"=>$t,
					"csh_uv"=>$u,
					"csh_send_article_number"=>$v,
					"csh_send_article_pv"=>$w,
					"csh_article_recommend_number"=>$x,
					"ctime"=>$ctime,
					"mark1"=>$l,
					"mark2"=>$m,
					"mark3"=>$n
				);
				$res=$maozhua_wb->insert("logs_monthly_data",$set);
				if($res){
					echo "<script>alert('添加成功！');window.location.href='/maozhua/import-excel-page?timestamp=".$timestamp."';</script>";
				}else{
					echo "<script>alert('添加失败！');window.location.href='/maozhua/import-excel-page';</script>";
					die;
				}
			}
		}else{
			echo "<script>alert('文件上传失败！请检查上传的文件是否符合要求。');window.location.href='/maozhua/import-excel-page';</script>";
		}
		die;
	}
	
	
	function importexcelMainbrandPageAction() {
		header("Content-type: text/html; charset=utf-8");
		$params = $this->_request->getParams();
		$this->view->title = "主营品牌Excel导入";
		$params_pool = array();
		$mod_tables = new Db_Joins_TablesJoins();
    	
		if($params['keyword'])
    	{
    		$mod_tables->keyword = $params['keyword'];
    		$this->view->keyword = $params['keyword'];
    		$params_pool[] = "keyword=".$params['keyword'];
    	}
//	
		if(!$params['page_id'])
    	{
    		$page_id = 1;
    	}else{
    		$page_id = $params['page_id'];
    	}
    	if($page_id)
    	{
    		$mod_tables->page_id = $page_id;
    		$this->view->page_id = $page_id;
    	}

		$this->view->data = $result = $mod_tables->GetMaozhuaMainBrand();
//		echo '<pre>';var_dump($result); echo '</pre>';die;		
		//make pagination
    	if(!empty($params_pool))
    	{
    		$params_string = implode("&", $params_pool);
    	}else{
    		$params_string = "";
    	}

    	if(1 == $page_id)
    	{
    		$first_page_string = "";
    		$previous_page_string = "";
    	}else{
    		$first_page_string = "<a href='?page_id=1&".$params_string."'>最前页</a>&nbsp;&nbsp;&nbsp;";
    		$previous_page_string = "<a href='?page_id=".($page_id-1)."&".$params_string."'>上一页</a>&nbsp;&nbsp;&nbsp;";
    	}

    	if($page_id < $result['total_pages'] )
    	{
    		$next_page_string = "<a href='?page_id=".($page_id+1)."&".$params_string."'>下一页</a>&nbsp;&nbsp;&nbsp;";
    		$last_page_string = "<a href='?page_id=".$result['total_pages']."&".$params_string."'>最后页</a>&nbsp;&nbsp;&nbsp;";
    	}else{
    		$next_page_string = "";
    		$last_page_string = "";
    	}

		$this->view->params_string = $params_string;
    	$this->view->pagination = $first_page_string.$previous_page_string.$next_page_string.$last_page_string;

		if($params['msg'])
		{
				switch ($params['msg'])
				{
					case 1:
						$this->view->msg = "更新成功";
						break;
					case 2:
						$this->view->msg = "更新失败";
						break;
					case 3:
						$this->view->msg = "新增成功";
						break;
					case 4:
						$this->view->msg = "新增失败";
						break;
					case 5:
						$this->view->msg = "删除成功";
						break;
					case 6:
						$this->view->msg = "删除失败";
						break;
				}
		}
		
	}
	
	function editMainBrandAction()
    {
        $params = $this->_request->getParams();
		$this->view->title='编辑主营品牌';
        $maozhua_wb = Zend_Registry::get('maozhua_wb');
        $select = $maozhua_wb->select();
        $select->from("main_brand",array("*"));
        $select->where("id = ?",$params['id']);
        $data = $maozhua_wb->fetchRow($select);
        $this->view->data = $data;
    }
	
	function editMainBrandSubmitAction()
    {
        $maozhua_wb = Zend_Registry::get('maozhua_wb');
        $params = $this->_request->getParams();
        $update = array(
            "dealer_name"=>$params['dealer_name'],
			"dealer_id"=>$params['dealer_id'],
            "primary_factory_name"=>$params['primary_factory_name'],
        );
        $where = "id=".$params['id'];
        $res=$maozhua_wb->update("main_brand",$update,$where);
		if($res){
			$this->_redirect("/maozhua/importexcel-mainbrand-page/msg/1");
		}else{
			$this->_redirect("/maozhua/importexcel-mainbrand-page/msg/2");
		}
    }
	
	function insertMainBrandAction()
    {
		$this->view->title='新增主营品牌';
    }
	
	function insertMainBrandSubmitAction()
    {
        $maozhua_wb = Zend_Registry::get('maozhua_wb');
        $params = $this->_request->getParams();
        $set = array(
            "dealer_name"=>$params['dealer_name'],
			"dealer_id"=>$params['dealer_id'],
            "primary_factory_name"=>$params['primary_factory_name'],
        );
        $res=$maozhua_wb->insert("main_brand",$set);
		if($res){
			$this->_redirect("/maozhua/importexcel-mainbrand-page/msg/3");
		}else{
			$this->_redirect("/maozhua/importexcel-mainbrand-page/msg/4");
		}
    }
	
	function delMainBrandAction()
    {
        $maozhua_wb = Zend_Registry::get('maozhua_wb');
        $params = $this->_request->getParams();
        $where='id = '.$params['id'];
        $res=$maozhua_wb->delete("main_brand",$where);
		if($res){
			$this->_redirect("/maozhua/importexcel-mainbrand-page/msg/5");
		}else{
			$this->_redirect("/maozhua/importexcel-mainbrand-page/msg/6");
		}
    }
	
	function importexcelMainbrandAction(){
		set_time_limit(3600);
		//判断是否选择了要上传的表格
		if (empty($_FILES['myfile']['name'])) {
			echo "<script>alert('您未选择表格');window.location.href='/maozhua/importexcel-mainbrand-page';</script>";
			die;
		}
		//获取表格的大小，限制上传表格的大小5M
		$file_size = $_FILES['myfile']['size'];
		if ($file_size>8*1024*1024) {
			echo "<script>alert('上传失败，上传的表格不能超过8M的大小');window.location.href='/maozhua/importexcel-mainbrand-page';</script>";
			die;
		}
		if (!empty($_FILES['myfile']['name'])) {
			$fileName = $_FILES['myfile']['name'];    //得到文件全名
			$dotArray = explode('.', $fileName);    //把文件名安.区分，拆分成数组
			$type = end($dotArray);
			if ($type != "xls" && $type != "xlsx") {
				echo "<script>alert('上传失败，只能上传Excel文件!');window.location.href='/maozhua/importexcel-mainbrand-page'</script>";
				die;
			}
		}
		//判断表格是否上传成功
		if (is_uploaded_file($_FILES['myfile']['tmp_name'])) {
			try {
				$maozhua_wb = Zend_Registry::get('maozhua_wb');
				$objPHPExcel = new PHPExcel();
				$objReader = PHPExcel_IOFactory::createReader('Excel5');

				//接收存在缓存中的excel表格
				$filename = $_FILES['myfile']['tmp_name'];
				$objPHPexcel = $objReader->load($filename); //$filename可以是上传的表格，或者是指定的表格
				$sheet = $objPHPexcel->getSheet(0);
				$highestRow = $sheet->getHighestRow(); // 取得总行数
			} catch (Exception $exc) {
				echo "<script>alert('读取文件失败！请查看上传文件的版本是否符合要求。');window.location.href='/maozhua/importexcel-mainbrand-page';</script>";
				die;
			}
			//循环读取excel表格,读取一条,插入一条
			//j表示从哪一行开始读取  从第二行开始读取，因为第一行是标题不保存
			for($j=2;$j<=$highestRow;$j++) 
			{
				$a = $objPHPexcel->getActiveSheet()->getCell("A".$j)->getValue();
				$b = $objPHPexcel->getActiveSheet()->getCell("B".$j)->getValue();
				$c = $objPHPexcel->getActiveSheet()->getCell("C".$j)->getValue();
				$set = array(
					"dealer_name"=>$a,
					"dealer_id"=>$b,
					"primary_factory_name"=>$c,
				);
				$res=$maozhua_wb->insert("main_brand",$set);
				if($res){
					echo "<script>alert('添加成功！');window.location.href='/maozhua/importexcel-mainbrand-page';</script>";
				}else{
					echo "<script>alert('添加失败！');window.location.href='/maozhua/importexcel-mainbrand-page';</script>";
					die;
				}
			}
		}else{
			echo "<script>alert('文件上传失败！请检查上传的文件是否符合要求。');window.location.href='/maozhua/import-excel-page';</script>";
		}
		die;
	}
	
	function everydayDealerDataAction()
	{
		$params = $this->_request->getParams();
		$this->view->title = "单店数据看板";

		$params_pool = array();
		$mod_tables = new Db_Joins_TablesJoins();

		if($params['date_start'])
		{
			$mod_tables->date_start = $params['date_start'];
			$this->view->date_start = $params['date_start'];
			$params_pool[] = "date_start=".$params['date_start'];
		}

		if($params['date_end'])
		{
			$mod_tables->date_end = $params['date_end'];
			$this->view->date_end = $params['date_end'];
			$params_pool[] = "date_end=".$params['date_end'];
		}

		if($params['city'])
		{
			$mod_tables->city = $params['city'];
			$this->view->city = $params['city'];
			$params_pool[] = "city=".$params['city'];
		}

		if($params['pingtai'])
		{
			$mod_tables->pingtai = $params['pingtai'];
			$this->view->pingtai = $params['pingtai'];
			$params_pool[] = "pingtai=".$params['pingtai'];
		}

		if($params['user_name'])
		{
			$mod_tables->user_name = $params['user_name'];
			$this->view->user_name = $params['user_name'];
			$params_pool[] = "user_name=".$params['user_name'];
		}

		if(!$params['page_id'])
		{
			$page_id = 1;
		}else{
			$page_id = $params['page_id'];
		}

		if($page_id)
		{
			$mod_tables->page_id = $page_id;
			$this->view->page_id = $page_id;
		}

		$this->view->data = $result = $mod_tables->GetEverydayDealerData();

		//make pagination
		if(!empty($params_pool))
		{
			$params_string = implode("&", $params_pool);
		}else{
			$params_string = "";
		}

		if(1 == $page_id)
		{
			$first_page_string = "";
			$previous_page_string = "";
		}else{
			$first_page_string = "<a href='?page_id=1&".$params_string."'>最前页</a>&nbsp;&nbsp;&nbsp;";
			$previous_page_string = "<a href='?page_id=".($page_id-1)."&".$params_string."'>上一页</a>&nbsp;&nbsp;&nbsp;";
		}

		if($page_id < $result['total_pages'] )
		{
			$next_page_string = "<a href='?page_id=".($page_id+1)."&".$params_string."'>下一页</a>&nbsp;&nbsp;&nbsp;";
			$last_page_string = "<a href='?page_id=".$result['total_pages']."&".$params_string."'>最后页</a>&nbsp;&nbsp;&nbsp;";
		}else{
			$next_page_string = "";
			$last_page_string = "";
		}

		$this->view->params_string = $params_string;
		$this->view->pagination = $first_page_string.$previous_page_string.$next_page_string.$last_page_string;
	}
	
	function everydayDealerMonitorAction()
	{
		$params = $this->_request->getParams();
		$this->view->title = "预警机制列表";

		$params_pool = array();
		$mod_tables = new Db_Joins_TablesJoins();

		if($params['date_start'])
		{
			$mod_tables->date_start = $params['date_start'];
			$this->view->date_start = $params['date_start'];
			$params_pool[] = "date_start=".$params['date_start'];
		}

		if($params['date_end'])
		{
			$mod_tables->date_end = $params['date_end'];
			$this->view->date_end = $params['date_end'];
			$params_pool[] = "date_end=".$params['date_end'];
		}

		if($params['city'])
		{
			$mod_tables->city = $params['city'];
			$this->view->city = $params['city'];
			$params_pool[] = "city=".$params['city'];
		}

		if($params['pingtai'])
		{
			$mod_tables->pingtai = $params['pingtai'];
			$this->view->pingtai = $params['pingtai'];
			$params_pool[] = "pingtai=".$params['pingtai'];
		}

		if($params['user_name'])
		{
			$mod_tables->user_name = $params['user_name'];
			$this->view->user_name = $params['user_name'];
			$params_pool[] = "user_name=".$params['user_name'];
		}

		if(!$params['page_id'])
		{
			$page_id = 1;
		}else{
			$page_id = $params['page_id'];
		}

		if($page_id)
		{
			$mod_tables->page_id = $page_id;
			$this->view->page_id = $page_id;
		}

		$this->view->data = $result = $mod_tables->GetEverydayDealerMonitor();

		//make pagination
		if(!empty($params_pool))
		{
			$params_string = implode("&", $params_pool);
		}else{
			$params_string = "";
		}

		if(1 == $page_id)
		{
			$first_page_string = "";
			$previous_page_string = "";
		}else{
			$first_page_string = "<a href='?page_id=1&".$params_string."'>最前页</a>&nbsp;&nbsp;&nbsp;";
			$previous_page_string = "<a href='?page_id=".($page_id-1)."&".$params_string."'>上一页</a>&nbsp;&nbsp;&nbsp;";
		}

		if($page_id < $result['total_pages'] )
		{
			$next_page_string = "<a href='?page_id=".($page_id+1)."&".$params_string."'>下一页</a>&nbsp;&nbsp;&nbsp;";
			$last_page_string = "<a href='?page_id=".$result['total_pages']."&".$params_string."'>最后页</a>&nbsp;&nbsp;&nbsp;";
		}else{
			$next_page_string = "";
			$last_page_string = "";
		}

		$this->view->params_string = $params_string;
		$this->view->pagination = $first_page_string.$previous_page_string.$next_page_string.$last_page_string;
	}
	
	public function exportEverydayDealerDataAction()
    {
        $params = $this->_request->getParams();
        $mod_tables = new Db_Joins_TablesJoins();
		
        if($params['date_start'])
		{
			$mod_tables->date_start = $params['date_start'];
		}

		if($params['date_end'])
		{
			$mod_tables->date_end = $params['date_end'];
		}

		if($params['city'])
		{
			$mod_tables->city = $params['city'];
		}

		if($params['pingtai'])
		{
			$mod_tables->pingtai = $params['pingtai'];
		}

		if($params['user_name'])
		{
			$mod_tables->user_name = $params['user_name'];
		}

        $result = $mod_tables->GetEverydayDealerData(1);
        $objPHPExcel = new PHPExcel();

		if (!empty($result)) {

			$objPHPExcel->setActiveSheetIndex(0)->setCellValue('A1', '日期');
			$objPHPExcel->setActiveSheetIndex(0)->setCellValue('B1', '客户');
			$objPHPExcel->setActiveSheetIndex(0)->setCellValue('C1', '城市');
			$objPHPExcel->setActiveSheetIndex(0)->setCellValue('D1', '主营品牌');
			$objPHPExcel->setActiveSheetIndex(0)->setCellValue('E1', '平台');
			$objPHPExcel->setActiveSheetIndex(0)->setCellValue('F1', '客户状态');
			$objPHPExcel->setActiveSheetIndex(0)->setCellValue('G1', '客户负责人');
			$objPHPExcel->setActiveSheetIndex(0)->setCellValue('H1', '同城同品牌商家数');
			$objPHPExcel->setActiveSheetIndex(0)->setCellValue('I1', '积分');
			$objPHPExcel->setActiveSheetIndex(0)->setCellValue('J1', '前三排名数量');
			$objPHPExcel->setActiveSheetIndex(0)->setCellValue('K1', '前六排名数量');
			$objPHPExcel->setActiveSheetIndex(0)->setCellValue('L1', '当日计划发文数量');
			$objPHPExcel->setActiveSheetIndex(0)->setCellValue('M1', '当日线索量');
			$objPHPExcel->setActiveSheetIndex(0)->setCellValue('N1', '上月平均线索量');
			$objPHPExcel->setActiveSheetIndex(0)->setCellValue('O1', '当月月累计线索量');
			$objPHPExcel->setActiveSheetIndex(0)->setCellValue('P1', '上月累计线索量');
			$objPHPExcel->setActiveSheetIndex(0)->setCellValue('Q1', '当日私有线索量');
			$objPHPExcel->setActiveSheetIndex(0)->setCellValue('R1', '当月累计私有线索量');
			$objPHPExcel->setActiveSheetIndex(0)->setCellValue('S1', 'PV量');
			$objPHPExcel->setActiveSheetIndex(0)->setCellValue('T1', '当月总PV量');
			$objPHPExcel->setActiveSheetIndex(0)->setCellValue('U1', '丢分项');
			$objPHPExcel->setActiveSheetIndex(0)->setCellValue('V1', '第一次丢分时间');
			$objPHPExcel->setActiveSheetIndex(0)->setCellValue('W1', '是否满分');
			$objPHPExcel->setActiveSheetIndex(0)->setCellValue('X1', '客户服务开始时间');
			$objPHPExcel->setActiveSheetIndex(0)->setCellValue('Y1', '客户服务结束时间');
			
			
			
			$n = 1;
			foreach($result['current_page_info'] as $key => $row)
			{
				if($row['status']==1){
					$row['status']='启用';
				}else{
					$row['status']='禁用';
				}
				if($row['account_type']==1){
					$row['account_type']='汽车之家';
				}else{
					$row['account_type']='易车';
				}
				$n++;
				$objPHPExcel->setActiveSheetIndex(0)->setCellValue('A' . $n, $row['datetime']);
				$objPHPExcel->setActiveSheetIndex(0)->setCellValue('B' . $n, $row['user_name']);
				$objPHPExcel->setActiveSheetIndex(0)->setCellValue('C' . $n, $row['city_name']);
				$objPHPExcel->setActiveSheetIndex(0)->setCellValue('D' . $n, $row['main_brand']);
				$objPHPExcel->setActiveSheetIndex(0)->setCellValue('E' . $n, $row['account_type']);
				$objPHPExcel->setActiveSheetIndex(0)->setCellValue('F' . $n, $row['status']);
				$objPHPExcel->setActiveSheetIndex(0)->setCellValue('G' . $n, $row['real_name']);
				$objPHPExcel->setActiveSheetIndex(0)->setCellValue('H' . $n, $row['city_factory_dealernum']);
				$objPHPExcel->setActiveSheetIndex(0)->setCellValue('I' . $n, $row['score']);
				$objPHPExcel->setActiveSheetIndex(0)->setCellValue('J' . $n, $row['top3_num']);
				$objPHPExcel->setActiveSheetIndex(0)->setCellValue('K' . $n, $row['top6_num']);
				$objPHPExcel->setActiveSheetIndex(0)->setCellValue('L' . $n, $row['plan_dispatch_num']);
				$objPHPExcel->setActiveSheetIndex(0)->setCellValue('M' . $n, $row['day_clues']);
				$objPHPExcel->setActiveSheetIndex(0)->setCellValue('N' . $n, $row['lastmonth_average_clues']);
				$objPHPExcel->setActiveSheetIndex(0)->setCellValue('O' . $n, $row['month_total_clues']);
				$objPHPExcel->setActiveSheetIndex(0)->setCellValue('P' . $n, $row['lastmonth_total_clues']);
				$objPHPExcel->setActiveSheetIndex(0)->setCellValue('Q' . $n, $row['day_private_clues']);
				$objPHPExcel->setActiveSheetIndex(0)->setCellValue('R' . $n, $row['month_total_private_clues']);
				$objPHPExcel->setActiveSheetIndex(0)->setCellValue('S' . $n, $row['pv']);
				$objPHPExcel->setActiveSheetIndex(0)->setCellValue('T' . $n, $row['month_total_pv']);
				$objPHPExcel->setActiveSheetIndex(0)->setCellValue('U' . $n, $row['lost_item']);
				$objPHPExcel->setActiveSheetIndex(0)->setCellValue('V' . $n, $row['first_lost_time']);
				$objPHPExcel->setActiveSheetIndex(0)->setCellValue('W' . $n, $row['is_full_mark']);
				$objPHPExcel->setActiveSheetIndex(0)->setCellValue('X' . $n, $row['service_start_time']);
				$objPHPExcel->setActiveSheetIndex(0)->setCellValue('Y' . $n, $row['service_end_time']);
			}
		}

		$rd = mt_rand(1000, 9999);
		$folder = "upload/reports/";
		$file_name = "reports-everyday-dealer-data-" . date("YmdHis") . $rd . ".xlsx";
		$objWriter = PHPExcel_IOFactory::createWriter($objPHPExcel, 'Excel2007');
		$objWriter->save($folder . $file_name);

		header("Location: /" . $folder . $file_name);

		echo "End.";
		die;

    }
	
	public function exportEverydayDealerMonitorAction()
    {
        $params = $this->_request->getParams();
        $mod_tables = new Db_Joins_TablesJoins();
		
        if($params['date_start'])
		{
			$mod_tables->date_start = $params['date_start'];
		}

		if($params['date_end'])
		{
			$mod_tables->date_end = $params['date_end'];
		}

		if($params['city'])
		{
			$mod_tables->city = $params['city'];
		}

		if($params['pingtai'])
		{
			$mod_tables->pingtai = $params['pingtai'];
		}

		if($params['user_name'])
		{
			$mod_tables->user_name = $params['user_name'];
		}

        $result = $mod_tables->GetEverydayDealerMonitor(1);
        $objPHPExcel = new PHPExcel();

		if (!empty($result)) {

			$objPHPExcel->setActiveSheetIndex(0)->setCellValue('A1', '日期');
			$objPHPExcel->setActiveSheetIndex(0)->setCellValue('B1', '客户');
			$objPHPExcel->setActiveSheetIndex(0)->setCellValue('C1', '城市');
			$objPHPExcel->setActiveSheetIndex(0)->setCellValue('D1', '平台');
			$objPHPExcel->setActiveSheetIndex(0)->setCellValue('E1', '私有线索');
			$objPHPExcel->setActiveSheetIndex(0)->setCellValue('F1', '前三占比');
			$objPHPExcel->setActiveSheetIndex(0)->setCellValue('G1', '出现丢分项');
			$objPHPExcel->setActiveSheetIndex(0)->setCellValue('H1', '登录情况');
			
			$n = 1;
			foreach($result['current_page_info'] as $key => $row)
			{
				if($row['account_type']==1){
					$row['account_type']='汽车之家';
				}else{
					$row['account_type']='易车';
				}
				$n++;
				$objPHPExcel->setActiveSheetIndex(0)->setCellValue('A' . $n, $row['datetime']);
				$objPHPExcel->setActiveSheetIndex(0)->setCellValue('B' . $n, $row['user_name']);
				$objPHPExcel->setActiveSheetIndex(0)->setCellValue('C' . $n, $row['city_name']);
				$objPHPExcel->setActiveSheetIndex(0)->setCellValue('D' . $n, $row['account_type']);
				$objPHPExcel->setActiveSheetIndex(0)->setCellValue('E' . $n, $row['day_clues_warn']);
				$objPHPExcel->setActiveSheetIndex(0)->setCellValue('F' . $n, $row['top3_warn']);
				$objPHPExcel->setActiveSheetIndex(0)->setCellValue('G' . $n, $row['lost_item_warn']);
				$objPHPExcel->setActiveSheetIndex(0)->setCellValue('H' . $n, $row['login_status_warn']);
			}
		}

		$rd = mt_rand(1000, 9999);
		$folder = "upload/reports/";
		$file_name = "reports-everyday-dealer-monitor-" . date("YmdHis") . $rd . ".xlsx";
		$objWriter = PHPExcel_IOFactory::createWriter($objPHPExcel, 'Excel2007');
		$objWriter->save($folder . $file_name);

		header("Location: /" . $folder . $file_name);

		echo "End.";
		die;

    }
	
	function appUsersListAction()
    {
		$params = $this->_request->getParams();
		$mod_tables = new Db_Joins_TablesJoins();

		$this->view->title = "云手机用户列表";
		if(!$params['page_id'])
    	{
    		$page_id = 1;
    	}else{
    		$page_id = $params['page_id'];
    	}

    	if($page_id)
    	{
			$mod_tables->page_id = $page_id;
    		$this->view->page_id = $page_id;
    	}
		
		if($params['keyword'])
		{
			$keyword=$params['keyword'];
			$mod_tables->keyword = $params['keyword'];
			$this->view->keyword = $params['keyword'];
		}
		if($params['status']==1){
			$mod_tables->status = 1;
			$this->view->status = 1;
		}elseif($params['status']==2){
			$mod_tables->status = 2;
			$this->view->status = 0;
		}
		
        $result = $mod_tables->GetMaozhuaAppUsersListData();
		$this->view->data = $result['data'];
		
		$total=$result['total'];
		$total_pages=$result['total_pages'];
		$this->view->total = $total;

    	if(1 == $page_id)
    	{
    		$first_page_string = "";
    		$previous_page_string = "";
    	}else{
    		$first_page_string = "<a href='?page_id=1'>最前页</a>&nbsp;&nbsp;&nbsp;";
    		$previous_page_string = "<a href='?page_id=".($page_id-1)."'>上一页</a>&nbsp;&nbsp;&nbsp;";
    	}

    	if($page_id < $total_pages )
    	{
    		$next_page_string = "<a href='?page_id=".($page_id+1)."'>下一页</a>&nbsp;&nbsp;&nbsp;";
    		$last_page_string = "<a href='?page_id=".$total_pages."'>最后页</a>&nbsp;&nbsp;&nbsp;";
    	}else{
    		$next_page_string = "";
    		$last_page_string = "";
    	}

    	$this->view->pagination = $first_page_string.$previous_page_string.$next_page_string.$last_page_string;
	}
	
	public function exportAppUsersListAction()
    {
        $params = $this->_request->getParams();
        $mod_tables = new Db_Joins_TablesJoins();
		
        if($params['keyword'])
		{
			$mod_tables->keyword = $params['keyword'];
		}
		if($params['status']==1){
			$mod_tables->status = 1;
		}elseif($params['status']==2){
			$mod_tables->status = 2;
		}

        $result = $mod_tables->GetMaozhuaAppUsersListExport();
        $objPHPExcel = new PHPExcel();

		if (!empty($result)) {

			$objPHPExcel->setActiveSheetIndex(0)->setCellValue('A1', '公司名称');
			$objPHPExcel->setActiveSheetIndex(0)->setCellValue('B1', '用户名');
			$objPHPExcel->setActiveSheetIndex(0)->setCellValue('C1', '电话号码');
			$objPHPExcel->setActiveSheetIndex(0)->setCellValue('D1', '创建时间');
			$objPHPExcel->setActiveSheetIndex(0)->setCellValue('E1', '状态');
			$n = 1;
			foreach($result as $key => $row)
			{
				if($row['status']==1){
					$row['status']='有效';
				}else{
					$row['status']='无效';
				}
				$n++;
				$objPHPExcel->setActiveSheetIndex(0)->setCellValue('A' . $n, $row['company']);
				$objPHPExcel->setActiveSheetIndex(0)->setCellValue('B' . $n, $row['user_name']);
				$objPHPExcel->setActiveSheetIndex(0)->setCellValue('C' . $n, $row['cellphone']);
				$objPHPExcel->setActiveSheetIndex(0)->setCellValue('D' . $n, $row['ctime']);
				$objPHPExcel->setActiveSheetIndex(0)->setCellValue('E' . $n, $row['status']);
			}
			
		}

		$rd = mt_rand(1000, 9999);
		$folder = "upload/reports/";
		$file_name = "reports-app-users-list-" . date("YmdHis") . $rd . ".xlsx";
		$objWriter = PHPExcel_IOFactory::createWriter($objPHPExcel, 'Excel2007');
		$objWriter->save($folder . $file_name);

		header("Location: /" . $folder . $file_name);

		echo "End.";
		die;

    }
	
	function handSendMessageAction()
    {
        $params = $this->_request->getParams();
		$this->view->title='消息推送';
		$maozhua_wb = Zend_Registry::get('maozhua_wb');
        $select = $maozhua_wb->select();
        $select->from('users', array("user_id","user_name"));
		$select->where('status = ?', 1);
		$user_data = $maozhua_wb->fetchAll($select);
        $this->view->data = $user_data;
//		echo '<pre>'; var_dump($user_data); echo '</pre>'; die;
    }
	
	function handSendMessageSubmitAction()
    {
        set_time_limit(3600);
		error_reporting(1);
		error_reporting(E_ALL);
		
		$params = $this->_request->getParams();
		$maozhua_wb = Zend_Registry::get('maozhua_wb');
		$mod_log = new Db_Tables_MpLogs();
		$mod_sys = new Db_Tables_Extensions();

		if($params['content']){
			$content = $params['content'].date("Y-m-d H:i:s");
			$content2 = $params['content'];
		}
		$user_ids=$params['user_ids'];
//		var_dump($user_ids);die;
		$result_pc=false;
		$result_wx=false;
		foreach ($user_ids as $key => $value) {
			if($value)
			{
				if($params['pc']){
					$mod_sys->PostWebSocketMsgToClient($value, $content);
					$result_pc=true;
				}
				
				if($params['wx']){
					$select_userwx = $maozhua_wb->select();
					$select_userwx->from('user_wxinfo', array("openid"));
					$select_userwx->where('user_id = ?', $value);
					$userwx = $maozhua_wb->fetchAll($select_userwx);
					foreach($userwx as $wx)
					{
						$mod_sys->sendTempleteMsg($wx['openid'],$content2);
					}
					$result_wx=true;
				}
				if($result_pc || $result_wx){
					$mod_log->AddLog($value, trim($content2), "handSendMessageSubmit");
				}else{
					$this->_redirect("/maozhua/hand-send-message?result=0");
				}
			}
		}
		$mod_log2 = new Db_Tables_Log2();
        $mod_log2->AddLog(24, "handSendMessageSubmit");
		
		$this->_redirect("/maozhua/hand-send-message?result=1");
    }
	
}